#!/usr/bin/perl
#
# Fetch the list of known Tor servers (from an existing Tor server) and
# display some of the basic info for each router.

##################################
#                                #
#  David Lladro - S2 Grupo       #
#     Noviembre 2011             #
#  Mail: david.lladro@gmail.com  #
#  Twitter: @davidlladro         #
#                                #
##################################

#
# Usage: ./tor.pl IP
#

use LWP::Simple;
use Term::ANSIColor;

# Hostname of an existing Tor router.  We use one of the directory authorities
# since that's pretty much what they're for.
$INITIAL_TOR_SERVER = "128.31.0.34";   # peacetime/moria1/moria2
$DIR_PORT = 9031;
my $flag = "false";
open(FILEHANDLE,">lista_tor.txt") or die 'cannot open file!';
print FILEHANDLE "Lista de routers de TOR\n";
# Fetch the list of servers
$content = get("http://$INITIAL_TOR_SERVER:$DIR_PORT/tor/status/all");
@lines = split /\n/,$content;

foreach $router (@lines) {
    if($router =~ m/^r\s+(\S+)\s+(\S+)\s+(\S+)\s+(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s+(\S+)\s+(\d+)\s+(\d+)$/) {
        ($name, $address, $or_port, $directory_port, $update_time) =
            ($1, $5, $6, $7, $4);
	print FILEHANDLE $address."\n";
	if($address eq $ARGV[0])
	{
		$flag = "true";
	}
    }
}

close(FILEHANDLE);
if($flag eq "true")
 {
        print color("green"), "+[OK] Es una ip de TOR\n", color("reset");
 }
else
 {
        print color("red"), "+[NO] No pertenece a la red de TOR\n", color("reset");
 }
